package com.audi.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.dto.SmartTriggerItem;
import com.audi.utils.Constants;

public class SmartTriggerNewAdapter extends BaseAdapter {

	private Activity mActivity;
	private ArrayList<SmartTriggerItem> mArrSmartTrigger;
	private SmartTriggerItem itemSmartTrigger;

	public SmartTriggerNewAdapter(Activity activity,
			ArrayList<SmartTriggerItem> arrSmartTrigger) {
		this.mActivity = activity;
		this.mArrSmartTrigger = arrSmartTrigger;
	}

	static class ViewHolder {
		ImageButton iBtnWhen, iBtnTrigger, iBtnControlOnOff, ibtn;
		TextView txtWhenDevice, txtWhenPlace, txtTriggerDevice,
				txtTriggerPlace, txtDescription;
		LinearLayout lnlRowSmartTrigger;
	}

	@Override
	public int getCount() {
		return mArrSmartTrigger.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mArrSmartTrigger.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		final int pos = position;
		itemSmartTrigger = mArrSmartTrigger.get(pos);
		LayoutInflater mInflater = (LayoutInflater) mActivity
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.row_action_smart_trigger,
					null);
			holder = new ViewHolder();
			holder.ibtn = (ImageButton) convertView
					.findViewById(R.id.ibtn_next);
			holder.iBtnControlOnOff = (ImageButton) convertView
					.findViewById(R.id.ibtn_control);
			holder.iBtnTrigger = (ImageButton) convertView
					.findViewById(R.id.ibtn_trigger);
			holder.iBtnWhen = (ImageButton) convertView
					.findViewById(R.id.ibtn_when);
			holder.txtWhenDevice = (TextView) convertView
					.findViewById(R.id.txt_when_device_name);
			holder.txtWhenPlace = (TextView) convertView
					.findViewById(R.id.txt_when_place_name);
			holder.txtTriggerDevice = (TextView) convertView
					.findViewById(R.id.txt_trigger_device_name);
			holder.txtTriggerPlace = (TextView) convertView
					.findViewById(R.id.txt_trigger_place_name);
			holder.txtDescription = (TextView) convertView
					.findViewById(R.id.txt_description);
			holder.lnlRowSmartTrigger = (LinearLayout) convertView
					.findViewById(R.id.lnl_row_action_smart_trigger);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (mArrSmartTrigger.get(pos).getIconControl() == 1) {
			holder.lnlRowSmartTrigger.setAlpha(0.3f);
			holder.iBtnControlOnOff
					.setImageResource(R.drawable.btn_place_card_onoff_off);

		} else {
			holder.lnlRowSmartTrigger.setAlpha(1f);
			holder.iBtnControlOnOff
					.setImageResource(R.drawable.btn_place_card_onoff_on);
		}

		holder.iBtnWhen.setImageResource(R.drawable.img_dishighlighted);
		holder.ibtn.setImageResource(R.drawable.btn_next);
		holder.iBtnTrigger.setImageResource(R.drawable.img_highlighted);
		holder.txtWhenDevice.setText(itemSmartTrigger.getWhenDeviceName());
		holder.txtWhenPlace.setText(itemSmartTrigger.getWhenDevicePlaceName());
		holder.txtTriggerDevice.setText(itemSmartTrigger.getTriggerDeviceName()
				.toString());
		holder.txtTriggerPlace.setText(itemSmartTrigger
				.getTriggerDevicePlaceName().toString());
		holder.txtDescription.setText(itemSmartTrigger.getSmarttriggername());
		holder.lnlRowSmartTrigger.setId(position);
		holder.iBtnControlOnOff.setTag(position);
		holder.iBtnControlOnOff.setId(position);

		switch (itemSmartTrigger.getWhenDeviceType()) {
		case 0:
		case 1:
		case 2:
		case 3:
			holder.iBtnWhen.setImageResource(R.drawable.detail_device);
			if (itemSmartTrigger.getWhenDeviceValues() == String
					.valueOf(Constants.ONOFF.OFF.ordinal())) {
				holder.iBtnWhen
						.setImageResource(R.drawable.btn_device_control_onoff_off);
			}
			break;
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
			holder.iBtnWhen
					.setImageResource(R.drawable.icon_warning_sensor_green);
			if (itemSmartTrigger.getWhenDeviceValues() == String
					.valueOf(Constants.SEN_WARNING.DETECT.ordinal())) {
				holder.iBtnWhen
						.setImageResource(R.drawable.icon_warning_sensor_red);
			}
			break;
		case 10:
			holder.iBtnWhen
					.setImageResource(R.drawable.icon_warning_sensor_green);
			if (itemSmartTrigger.getWhenDeviceValues() == String
					.valueOf(Constants.SEN_WARNING.DETECT.ordinal())) {
				holder.iBtnWhen.setImageResource(R.drawable.icon_lamp_large);
			}
			break;
		case 11:
			holder.iBtnWhen.setImageResource(R.drawable.item_device_status);
			// Xet cac truong hop dat icon
			/*
			 * if(itemSmartTrigger.getWhenDeviceValue()==Constants.TEM_WARNING.LESS
			 * .ordinal()){ holder.iBtnWhen
			 * .setImageResource(R.drawable.btn_device_control_warning_xanhduong
			 * ); } else
			 * if(itemSmartTrigger.getWhenDeviceValue()==Constants.TEM_WARNING
			 * .EXCEEDED .ordinal()){ holder.iBtnWhen
			 * .setImageResource(R.drawable.btn_device_control_warning_cam); }
			 */
			break;
		case 12:
			holder.iBtnWhen.setImageResource(R.drawable.item_device_status);
			// Xet cac truong hop dat icon
			/*
			 * if(itemSmartTrigger.getWhenDeviceValue()==Constants.HUM_STATUS.LESS
			 * .ordinal()){ holder.iBtnWhen
			 * .setImageResource(R.drawable.btn_device_control_warning_xanhduong
			 * ); } else
			 * if(itemSmartTrigger.getWhenDeviceValue()==Constants.HUM_STATUS
			 * .EXCEEDED .ordinal()){ holder.iBtnWhen
			 * .setImageResource(R.drawable.btn_device_control_warning_cam); }
			 */
			break;
		case 13:
			// Xet cac truong hop dat icon
			/*
			 * holder.iBtnWhen.setImageResource(R.drawable.btn_door_opened);
			 * if(itemSmartTrigger
			 * .getWhenDeviceValue()==Constants.DOOR_STATUS.DOOR_CLOSE
			 * .ordinal()){ holder.iBtnWhen
			 * .setImageResource(R.drawable.btn_door_closed); }
			 */
			break;
		case 14:
			// Xet cac truong hop dat icon
			/*
			 * holder.iBtnWhen.setImageResource(R.drawable.btn_door_closed);
			 * if(itemSmartTrigger
			 * .getWhenDeviceValue()==Constants.DOOR_STATUS.DOOR_OPEN
			 * .ordinal()){ holder.iBtnWhen
			 * .setImageResource(R.drawable.btn_door_opened); }
			 */
			break;
		case 15:
			// Xet cac truong hop dat icon
			/*
			 * holder.iBtnWhen
			 * .setImageResource(R.drawable.btn_device_control_warning_no);
			 * if(itemSmartTrigger
			 * .getWhenDeviceValue()==Constants.SEN_WARNING.DETECT .ordinal()){
			 * holder.iBtnWhen
			 * .setImageResource(R.drawable.btn_device_control_warning_xam); }
			 */
			break;
		default:
			break;
		}

		switch (itemSmartTrigger.getTriggerDeviceType()) {
		case 0:
		case 1:
		case 2:
		case 3:
			holder.iBtnTrigger.setImageResource(R.drawable.detail_device);
			if (itemSmartTrigger.getTriggerDeveceValues() == String
					.valueOf(Constants.ONOFF.OFF.ordinal())) {
				holder.iBtnTrigger
						.setImageResource(R.drawable.btn_device_control_onoff_off);
			}
			break;
		case 10:
			holder.iBtnTrigger
					.setImageResource(R.drawable.icon_warning_sensor_green);
			if (itemSmartTrigger.getTriggerDeveceValues() == String
					.valueOf(Constants.SEN_WARNING.DETECT.ordinal())) {
				holder.iBtnTrigger.setImageResource(R.drawable.icon_lamp_large);
			}
			break;
		default:
			break;
		}

		if (itemSmartTrigger.getIconControl() == 0) {
			holder.iBtnControlOnOff
					.setImageResource(R.drawable.btn_place_card_onoff_on);
		} else if (itemSmartTrigger.getIconControl() == 1) {
			holder.iBtnControlOnOff
					.setImageResource(R.drawable.btn_place_card_onoff_off);
			holder.lnlRowSmartTrigger.setAlpha(0.3f);
		}
		holder.iBtnControlOnOff.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				int foo = (Integer) arg0.getId();
				SmartTriggerItem item = new SmartTriggerItem();
				item = mArrSmartTrigger.get(foo);
				int onoff = item.getIconControl();
				if (onoff == 0) {
					holder.lnlRowSmartTrigger.setAlpha(0.3f);
					holder.iBtnControlOnOff
							.setImageResource(R.drawable.btn_place_card_onoff_off);
					item.setIconControl(1);
					notifyDataSetChanged();

				} else {
					holder.lnlRowSmartTrigger.setAlpha(1f);
					holder.iBtnControlOnOff
							.setImageResource(R.drawable.btn_place_card_onoff_on);
					item.setIconControl(0);
					notifyDataSetChanged();
				}
			}
		});

		holder.lnlRowSmartTrigger.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				try {
					Toast.makeText(mActivity, "Continue Class",
							Toast.LENGTH_SHORT).show();
				} catch (Exception e) {

				}

			}
		});

		return convertView;
	}

}
